import java.util.Scanner;
import java.util.Arrays;//工具包

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-08-19
 * Time: 10:47
 */
public class ArrayDemo {
    /**
     * 汉诺塔问题递归实现
     * @param args 1
     */
    public static void move(char posx, char posy){
        System.out.println(posx + " -> " + posy);
    }

    /**
     *
     * @param n    盘子个数
     * @param pos1 盘子初始位置
     * @param pos2 盘子中转位置
     * @param pos3 盘子结束位置
     */
    public static void hanoi(int n, char pos1, char pos2, char pos3){
        if (1 == n){
            move(pos1, pos3);
        } else{
            hanoi(n - 1, pos1, pos3, pos2);
            move(pos1, pos3);
            hanoi(n - 1, pos2, pos1, pos3);
        }
    }
    public static void main0(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()){
            int n = scanner.nextInt();
            hanoi(n, 'A', 'B', 'C');
        }
    }

    //java 数组

    //数组：存放相同数据类型的数据的集合
    //int[] array = { 1 2 3 4 5 6 7 }
    //double[] array

    //1.定义数组的时候，括号内不能加数据
    public static void main2(String[] args) {
        int[] array = { 1,2,3,4,5,6,7 };
        int[] array2 = new int[3];//未初始化,给定三个空间
        //关键字：实例化一个对象
        //意味着：java当中的数组，也是一个对象
        int[] array3 = new int[]{ 1,2,3,4,5 };
    }

    public static void main3(String[] args) {
        int[] array = { 1,2,3,4,5,6,7 };
        System.out.println(array.length);
        array[3] = 13;
        System.out.println(array[3]);//ArrayIndexOutOfBoundsException - 数组越界异常
    }

    //for 与 foreach循环
    //1.for循环是可以拿到下标的
    //2.foreach是拿不到下标的 - 集合中
    public static void main4(String[] args) {
        int[] array = { 1,2,3,4,5,6 };
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();

        //增强for循环 - foreach
        for (int a :
             array) {
            System.out.print(a + " ");
        }
        System.out.println();
        //3.借助Java的操作数组的工具类 Arrays.toString :
        //将参数的数组 - 一字符串的形式进行打印
        String ret = Arrays.toString(array);
        System.out.println(ret);
    }

    //1.本地方法栈 - C.C++代码
    //2.方法区 - 静态的变量
    //3.堆 - 对象
    //4.java虚拟机栈
    //5.程序计数器
    public static void main5(String[] args) {
        int[] array = { 1,2,3,4,5,6 };
        //array - 变量存放在栈上 - 引用变量存放地址
        //1,2,3,4,5,6 - 对象 - 存放在堆上
        //引用变量指向一个对象
        int[] array2 = null;
        //表明这个引用不指向任何对象
        System.out.println(array2.length);//空指针异常
        //一发现这个空指针异常，去找哪个引用是空的
    }

    //按引用传递 -- 按值(地址)传递
    public static void printf(int[] array){
        System.out.println(Arrays.toString(array));
    }
    public static void main6(String[] args) {
        int[] array = { 1,2,3,4,5,6 };
        printf(array);
    }

    public  static void func1(int[] array){
        //修改形参指向的对象
        array = new int[]{ 11,2,13,4,51,61 };
    }
    public static void func2(int[] array){
        //修改实参的对象
        array[0] = 899;
    }

    public static void main7(String[] args) {
        int[] array = { 1,2,3,4,5,6 };
        System.out.println(Arrays.toString(array));
        //func1(array);
        func2(array);
        System.out.println(Arrays.toString(array));
    }

    public static void main8(String[] args) {
        int[] array1 = { 1,2,3,4,5 };
        int[] array2 = array1;
        //代表array2这个引用 指向了array1这个引用指向的对象
        //引用指向引用：错误的描述，引用只能指向对象
        //一个引用能不能指向多个对象 - 不能 - 一个引用只能保存一个对象的地址
    }

    //引用不一定在栈上：一个变量在不在栈上，是你变量的性质决定的，如果是一个局部变量（栈），实例成员变量（不一定在栈）

    public static void swap(int[] array){
        int tmp = array[0];
        array[0] = array[1];
        array[1] = tmp;
    }
    public static void main9(String[] args) {
        int[] array = {10, 20};
        System.out.println("交换前：" + array[0] + " " + array[1]);
        swap(array);
        System.out.println("交换后：" + array[0] + " " + array[1]);
    }

    /**
     * 在原来的数组上扩大2倍
     * @param array
     */
    public static void func(int[] array){
        for (int i = 0; i < array.length; i++) {
            array[i] = 2*array[i];
        }
    }
    public static int[] transform(int[] array){
        int[] ret = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            ret[i] = 2*array[i];
        }
        return ret;
    }
    public static void main10(String[] args) {
        int[] array = { 1,2,3,4,5 };
        //func(array);
        System.out.println(Arrays.toString(transform(array)));
    }

    public static String myToString(int[] array){
        if (array == null) return "null";
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret += array[i];
            if (i != array.length - 1){
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }
    public static void main11(String[] args) {
        int[] array = { 1,2,3,4,5 };
        System.out.println(myToString(array));
    }
    //了解一下 idea断言设置

    //找数组元素的最大值
    public static int maxNum(int[] array){
        if (array == null || array.length == 0) return -1;//业务上的处理
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (max < array[i]){
                max = array[i];
            }
        }
        return max;
    }
    public static void main12(String[] args) {
        int[] array = { 12,8,1,2,10 };
        System.out.println(maxNum(array));
    }

    //查找数组元素
    public static int findNum(int[] array, int key){
        for (int i = 0; i < array.length; i++) {
            if (array[i] == key){
                return i;
            }
        }
        return -1;
    }
    public static void main13(String[] args) {
        int[] array = { 1,9,4,2,15,17 };
        System.out.println(findNum(array, 12));
    }

    public static void main14(String[] args) {
        int[] array = { 1,2,3,4,15,17 };
        //二分查找函数
        System.out.println(Arrays.binarySearch(array, 3));
    }

    //判断数组的有序性
    public static boolean isUp(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            if (array[i] > array[i + 1]){
                return false;
            }
        }
        return true;
    }
    public static void main15(String[] args) {
        int[] array = { 11, 12, 13, 14 ,15 };
        boolean flag = isUp(array);
        System.out.println(flag);
    }

    //冒泡排序 - 优化
    //第一个i最后一个时已经有序，故不需要比较
    public static void bubble_sort(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]){
                    int tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                    flg = true;
                }
            }
            if (flg == false){
                return;
            }
        }
    }
    public static void main16(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = scanner.nextInt();
        }
        Arrays.sort(array);
        //bubble_sort(array);
        System.out.println(Arrays.toString(array));
    }

    //填充函数 Array.file
    public static void main17(String[] args) {
        int[] array = new int[10];
        Arrays.fill(array, 2, 5, 1999);
        System.out.println(Arrays.toString(array));
    }

    //数组逆序
    public static void reverse(int[] array){
        int left = 0;
        int right = array.length - 1;
        while (left < right){
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }
    }
    public static void main18(String[] args) {
        int[] array = { 1,2,3,4,5 };
        reverse(array);
        System.out.println(Arrays.toString(array));
    }

    //数组数字排列
    public static void exSort(int[] array){
        int left = 0;
        int right = array.length - 1;
        while (left < right){
            while (array[left] % 2 == 0 && left < right){
                left++;
            }
            while (array[right] % 2 != 0 && left < right){
                right--;
            }
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }
    }
    public static void main19(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = scanner.nextInt();
        }
        exSort(array);
        System.out.println(Arrays.toString(array));
    }

    //数组拷贝
    //深拷贝：对指针+对象的拷贝，拷贝后是指向两个不同地址的指针
    //浅拷贝：对指针的拷贝，拷贝后指向同一片内存空间 - 以下四种均为浅拷贝
    public static int[] copyArray(int[] array){
        int[] copy = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }
        return copy;
    }
    public static void main20(String[] args) {
        int[] array = { 1,2,3,4,5,6 };
        //int[] ret = copyArray(array);
        //System.out.println(Arrays.toString(array));

        //int[] ret = Arrays.copyOf(array, array.length * 2);//拷贝整个数组

        //int[] ret = Arrays.copyOfRange(array, 1, 3);//拷贝局部

        //int[] ret = new int[array.length];
        //System.arraycopy(array, 0, ret, 2,array.length - 2);//拷贝范围

        int[] copy = array.clone();//产生一个副本
        System.out.println(Arrays.toString(copy));
    }

    //二维数组
    public static void main21(String[] args) {
        int[][] array = {{1,2,3},{4,5,6}};
        int[][] array2 = new int[][]{{1,2,3},{4,5,6}};
        int[][] array3 = new int[2][3];
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("+++++++++++++++");
        for (int[] ret:
             array) {
            for (int x:
                 ret) {
                System.out.print(x + " ");
            }
            System.out.println();
        }
        System.out.println("+++++++++++++++");
        System.out.println(Arrays.deepToString(array));//二维数组打印函数
    }

    public static void main(String[] args) {
        int[][] array = {{1,2},{4,5,6}};
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("+++++++++++++++");

        int[][] array2 = new int[2][];
        //表明形成的两个空指针是null

        //不规则的二维数组
        array2[0] = new int[3];
        array2[1] = new int[2];
        for (int i = 0; i < array2.length; i++) {
            for (int j = 0; j < array2[i].length; j++) {
                System.out.print(array2[i][j] + " ");
            }
            System.out.println();
        }
    }
}































